package com.drinks.yiniang.ui.activity.user.wallet;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.drinks.yiniang.R;
import com.drinks.yiniang.entity.user.DrawCashInfoJson;
import com.drinks.yiniang.io.api.UserApiIO;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.entity.BaseJson;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.ui.widget.textview.ClearEditText;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;
import com.hansen.library.utils.StringUtils;

/**
 * 提现页面
 *
 * @author han
 * @date 2019/09/06
 */
public class DrawCashActivity extends BaseTranBarActivity implements OnNavgationBarClickListener {

    private final int REQ_EDIT_ACCOUNT_CODE = 0x01;

    //nav bar
    private NavigationBarLayout nav_bar_withdraw;
    private ConstraintLayout cstl_drawcash_arrow;
    private ImageView iv_draw_cash_account_icon;
    //输入提现金额
    private ClearEditText et_withdraw_cash_money;
    //添加支付宝
    private TextView tv_drawcash_add_aliaccount;
    //提现账号姓名
    private TextView tv_draw_cash_account_name;
    //提现账号
    private TextView tv_draw_cash_account;
    //可提现金额
    private TextView tv_withdraw_cash_balance;
    //全部提现
    private TextView tv_withdraw_cash_all;
    //可提现日期
    private TextView tv_drawcash_valid_date;
    //执行提现
    private Button btn_withdraw_cash;

    //总金额
    private String mTotalMoney;

    @Override
    protected void initView() {
        nav_bar_withdraw = findViewById(R.id.nav_bar_withdraw);

        cstl_drawcash_arrow = findViewById(R.id.cstl_drawcash_arrow);

        iv_draw_cash_account_icon = findViewById(R.id.iv_draw_cash_account_icon);

        et_withdraw_cash_money = findViewById(R.id.et_withdraw_cash_money);

        tv_drawcash_add_aliaccount = findViewById(R.id.tv_drawcash_add_aliaccount);
        tv_draw_cash_account_name = findViewById(R.id.tv_draw_cash_account_name);
        tv_draw_cash_account = findViewById(R.id.tv_draw_cash_account);
        tv_withdraw_cash_balance = findViewById(R.id.tv_withdraw_cash_balance);
        tv_withdraw_cash_all = findViewById(R.id.tv_withdraw_cash_all);
        tv_drawcash_valid_date = findViewById(R.id.tv_drawcash_valid_date);

        btn_withdraw_cash = findViewById(R.id.btn_withdraw_cash);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        getDrawCashInfo();
    }

    @Override
    protected void initListener() {
        nav_bar_withdraw.setOnNavgationBarClickListener(this);
        cstl_drawcash_arrow.setOnClickListener(this);
        tv_withdraw_cash_all.setOnClickListener(this);
        btn_withdraw_cash.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.cstl_drawcash_arrow://添加支付宝信息
                Intent intent = new Intent(mContext, EditDrawCashAccountActivity.class);
                startActivityForResult(intent, REQ_EDIT_ACCOUNT_CODE);
                break;
            case R.id.tv_withdraw_cash_all://全部体现
                et_withdraw_cash_money.setText(mTotalMoney);
                if(mTotalMoney != null) et_withdraw_cash_money.setSelection(mTotalMoney.length());
                break;
            case R.id.btn_withdraw_cash://执行提现
                applyDrawCash();
                break;
            default:
                break;
        }
    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_draw_cash;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Constants.RESULT_CODE_NORMAL && requestCode == REQ_EDIT_ACCOUNT_CODE) {
            getDrawCashInfo();
        }
    }

    //申请提现
    private void applyDrawCash() {
        if(StringUtils.isEmpty(StringUtils.getTextViewText(tv_draw_cash_account.getText()))) {
            ToastUtils.showShort(R.string.text_hint_add_alipay_account);
            return;
        }

        String money = StringUtils.getEditTextString(et_withdraw_cash_money.getText());

        if (StringUtils.isEmpty(money)) {
            ToastUtils.showShort(R.string.text_hint_input_withdraw_money);
            return;
        }

        if (!StringUtils.isGratherZeroMoney(money)) {
            ToastUtils.showShort(R.string.text_hint_input_true_money);
            return;
        }

        if(!StringUtils.judgeGreaterNumber(mTotalMoney, money)) {
            ToastUtils.showShort(R.string.text_balance_is_not_enough);
            return;
        }

        showLoadingDialog();

        UserApiIO.getInstance().doApplyDrawCash(money, new APIRequestCallback<BaseJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(BaseJson res) {
                ToastUtils.showShort(R.string.text_apply_success);
                setResult(Constants.RESULT_CODE_NORMAL);
                finish();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    //获取提现相关信息
    private void getDrawCashInfo() {
        showLoadingDialog();

        UserApiIO.getInstance().getDrawCashInfo(new APIRequestCallback<DrawCashInfoJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(DrawCashInfoJson res) {
                setData(res.getData());
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    //设置数据
    private void setData(DrawCashInfoJson.DrawCashInfoData data) {
        StringBuilder builder = new StringBuilder();

        builder.append(getString(R.string.text_can_drawcash_date_colon));
        builder.append(StringUtils.getNullEmptyConvert__(data.getAble_withdraw_start_date()));
        builder.append("~");
        builder.append(StringUtils.getNullEmptyConvert__(data.getAble_withdraw_end_date()));

        mTotalMoney = StringUtils.getNullEmptyConvertZeroPrice(data.getBalance_amount());

        tv_withdraw_cash_balance.setText(mTotalMoney);
        tv_drawcash_valid_date.setText(builder);

        if (StringUtils.isEmpty(data.getAlipay_account())) {//未填写支付宝信息
            iv_draw_cash_account_icon.setVisibility(View.INVISIBLE);
            tv_draw_cash_account_name.setVisibility(View.INVISIBLE);
            tv_draw_cash_account.setVisibility(View.INVISIBLE);

            tv_drawcash_add_aliaccount.setVisibility(View.VISIBLE);
        } else {
            tv_drawcash_add_aliaccount.setVisibility(View.GONE);

            iv_draw_cash_account_icon.setVisibility(View.VISIBLE);
            tv_draw_cash_account_name.setVisibility(View.VISIBLE);
            tv_draw_cash_account.setVisibility(View.VISIBLE);

            tv_draw_cash_account_name.setText(data.getAlipay_name());
            tv_draw_cash_account.setText(getString(R.string.text_alipay_account_colon) + data.getAlipay_account());
        }
    }
}
